package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.BusinessAbodeBean;
import com.koron.css2.baseConfig.bean.TradeClassifyBean;
import com.koron.css2.baseConfig.mapper.BusinessAbodeMapper;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.baseConfig.mapper.TradeClassifyMapper;
import com.koron.css2.clientCenter.bean.*;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.clientCenter.mapper.UserPersonDetailMapper;
import com.koron.css2.meterService.MeterService;
import com.koron.css2.meterService.dto.MsInfoDto;
import com.koron.css2.meterService.mapper.MsChgPlanDetailMapper;
import com.koron.css2.meterService.mapper.MsInfoMapper;
import com.koron.css2.serviceManage.bean.DistrictBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.DistrictMapper;
import com.koron.css2.serviceManage.mapper.PubAdminAreaMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.ADOConnection;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 用户信息查询，根据用户信息查询出账户、客户等信息
 *
 * @author 甄玉晴
 *
 */
public class UserSelect implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<UserDetailInfoBean> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功",
				UserDetailInfoBean.class);
		try {
			UserBean bean = JsonUtils.objectToPojo(req.getData(), UserBean.class);
			UserInfoMapper mapper = factory.getMapper(UserInfoMapper.class);
			//MsInfoMapper msMapper = factory.getMapper(MsInfoMapper.class);
			if (bean == null) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "查询条件不能为空不能为空", null);
			}
			PubAdminAreaMapper pmapper = factory.getMapper(PubAdminAreaMapper.class,"_default");
			boolean reuse=false;
			if("8".equals(bean.getStatus())) {
				bean.setStatus(null);
				reuse=true;
			}
			boolean stop=false;
			if("9".equals(bean.getStatus())) {
				bean.setStatus(null);
				stop=true;
			}
			boolean guohui=false;
			if("10".equals(bean.getStatus())) {
				bean.setStatus(null);
				guohui=true;
			}
			boolean SalesAccount=false;
			if("7".equals(bean.getStatus())) {
				bean.setStatus(null);
				SalesAccount=true;
			}
			boolean change=false;
			if("6".equals(bean.getStatus())) {
				bean.setStatus(null);
				change=true;
			}
			boolean redflush=false;
			if("5".equals(bean.getStatus())) {
				bean.setStatus(null);
				redflush=true;
			}
			UserBean user = mapper.getUserInfo(bean, UserBean.USER);// 获取用户信息
			if(user == null) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "无该用户信息", null);
			}
			if(reuse) {//如果是复用
				if("1".equals(user.getStatus())) {
					return MessageBean.create(Constant.ILLEGAL_PARAMETER, "该用户正常使用中，无法复用", null);
				}
				if("0".equals(user.getStatus())) {
					return MessageBean.create(Constant.ILLEGAL_PARAMETER, "该用户已注销，无法复用", null);
				}

			}
			if(stop){ //停用
				if(Objects.equals("0", user.getStatus())) {
					return MessageBean.create(Constant.ILLEGAL_PARAMETER, "该用户已销户", null);
				}
				if(Objects.equals("2", user.getStatus())){
					return MessageBean.create(Constant.ILLEGAL_PARAMETER, "该用户已停用", null);
				}
			}
			if(guohui){ //过户
				if(Objects.equals("0", user.getStatus())){
					return MessageBean.create(Constant.ILLEGAL_PARAMETER, "该用户已销户", null);
				}
				if(Objects.equals("2", user.getStatus())){
					return MessageBean.create(Constant.ILLEGAL_PARAMETER, "该用户已停用", null);
				}
			}
			if(SalesAccount){ //销户
				if(Objects.equals("0", user.getStatus())){
					return MessageBean.create(Constant.ILLEGAL_PARAMETER, "该用户已销户", null);
				}
			}
			if(change){ //变更、快捷变更
				if("0".equals(user.getStatus())){
					return MessageBean.create(Constant.ILLEGAL_PARAMETER, "该用户已销户", null);
				}
				if("2".equals(user.getStatus())){
					return MessageBean.create(Constant.ILLEGAL_PARAMETER, "该用户已停用", null);
				}
			}

			if (user == null) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "无该用户信息或该用户不符合条件", null);
			}
			if(redflush){
				if("0".equals(user.getStatus())){
					return MessageBean.create(Constant.ILLEGAL_PARAMETER, "该用户已销户，无法做实收减免操作", null);
				}
				if("2".equals(user.getStatus())){
					return MessageBean.create(Constant.ILLEGAL_PARAMETER, "该用户已停用，无法做实收减免操作", null);
				}
			}



			if(bean.getStatus() != null && "10".equals(bean.getStatus())) {
				MsInfoMapper msInfoMapper = factory.getMapper(MsInfoMapper.class);
				MsInfoDto msInfoDto = msInfoMapper.getMsInfoByNo(user.getMeterNo());
				MsChgPlanDetailMapper msChgPlanDetailMapper = factory.getMapper(MsChgPlanDetailMapper.class);
				if(msInfoDto!=null && ("3".equals(msInfoDto.getMeterStatus()) ||  "4".equals(msInfoDto.getMeterStatus()))) {
					return MessageBean.create(Constant.ILLEGAL_PARAMETER, "该用户水表已拆表", null);
				}
				List<String> meterNos = msChgPlanDetailMapper.selectProcessNotEndList(user.getMeterNo());
				if(meterNos!=null && meterNos.size()>0)
					return MessageBean.create(Constant.ILLEGAL_PARAMETER, "该水表存在未完成的拆表流程", null);
			}
			if(!StringUtils.isEmpty(user.getClientContractAddr())) {
				user.setClientContractAddrId(user.getClientContractAddr());
				String area = pmapper.selectFullNameById(user.getClientContractAddr());
				user.setClientContractAddr(area);
			}
			// 获取客户信息
			CtmInfoMapper ctmMapper = factory.getMapper(CtmInfoMapper.class);
			CtmInfoBean ctm = ctmMapper.getCtmInfoByCtmNo(user.getCtmNo(), CtmInfoBean.CTM);
			if(!StringUtils.isEmpty(ctm.getClientAddr())) {
				String area = pmapper.selectFullNameById(ctm.getClientAddr());
				ctm.setClientAddr(area);
			}
			// 获取账户信息
			AccountInfoMapper accountMapper = factory.getMapper(AccountInfoMapper.class);
			AccountInfoBean account = accountMapper.getAccountInfoByCode(user.getAccountNo(), AccountInfoBean.ACCOUNT);
			// 获取水表信息
			MsInfoDto msInfo=null;
			if (!StringUtils.isEmpty(user.getMeterNo())) {
				MeterService meterSrvice = new MeterService();
				msInfo = ADOConnection.runTask("_default", meterSrvice, "getMsInfoByNo", MsInfoDto.class,
						user.getMeterNo(),userInfo.getCurWaterCode());
			}
			// 获取用水人口信息
			UserPersonDetailMapper personMapper = factory.getMapper(UserPersonDetailMapper.class);
			List<UserPersonDetailBean> persons = personMapper.getPersonDetailByUserNo(bean.getUserNo());
			// 获取营业所名称
			if (!StringUtils.isEmpty(user.getBusinessAbode())) {
				BusinessAbodeMapper busniessAbodemapper = factory.getMapper(BusinessAbodeMapper.class);
				BusinessAbodeBean abode = busniessAbodemapper.findBusinessAbodeById(user.getBusinessAbode());
				user.setBusinessAbodeName(abode==null?"":abode.getAbodeName());
			}
			// 获取营业区域
			if (!StringUtils.isEmpty(user.getBusinessArea())) {
				BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
				user.setBusinessAreaName(
						businessAreaMapper.findBusinessAreaById(user.getBusinessArea(), userInfo.getCurWaterCode()));
			}
			// 获取行政区域
			if (!StringUtils.isEmpty(user.getAdminArea())) {
				DistrictMapper districtMapper = factory.getMapper(DistrictMapper.class);
				DistrictBean district = districtMapper.findDistrictById(user.getAdminArea());
				user.setAdminAreaName(district==null?"":district.getSname());
			}
			// 获取行业分类名称
			if (!StringUtils.isEmpty(user.getTradeClassify())) {
				TradeClassifyMapper tradeMapper = factory.getMapper(TradeClassifyMapper.class);
				TradeClassifyBean trade = tradeMapper.selectTradeClassifyById(user.getTradeClassify());
				user.setTradeClassifyName(trade==null?"":trade.getTradeName());
			}
			if (account != null && StringUtils.isNotBlank(account.getAccountType())) {
				account.setAccountTypeName(Tools.getDicNameByCodeAndValue(factory, "ACT", account.getAccountType()));
			}
			if (account != null && StringUtils.isNotBlank(account.getPayWay())) {
				account.setPayWayName(Tools.getDicNameByCodeAndValue(factory, "BPW", account.getPayWay()));
			}
			if (account != null && StringUtils.isNotBlank(account.getInvoiceType())) {
				account.setInvoiceTypeName(Tools.getDicNameByCodeAndValue(factory, "BBT", account.getInvoiceType()));
			}
			if (account != null && StringUtils.isNotBlank(account.getDeliverWay())) {
				account.setDeliverWayName(Tools.getDicNameByCodeAndValue(factory, "SDW", account.getDeliverWay()));
			}
//			// 获取用水类型
//			if (!StringUtils.isBlank(user.getUseWaterType())) {
//				WaterTypeMapper waterTypeMapper = factory.getMapper(WaterTypeMapper.class);
//				WaterTypeBean waterType = waterTypeMapper.findWaterTypeById(user.getUseWaterType());
//				user.setUseWaterTypeName(waterType==null?"":waterType.getWaterTypeName());
//			}
			//user字段转换
			Map<String, String> cus = Tools.mapDicByCode(factory, "CUS");//用户状态
			Map<String, String> mrm = Tools.mapDicByCode(factory, "MRM");//拆表类型
			Map<String, String> MMT = Tools.mapDicByCode(factory, "MMT");//水表类型
			Map<String, String> uws = Tools.mapDicByCode(factory, "UWS");//用水对象
			Map<String, String> cmc = Tools.mapDicByCode(factory, "CMC");//水表用途
			Map<String, String> csw = Tools.mapDicByCode(factory, "CSW");//分摊方式
			Map<String, String> ctl = Tools.mapDicByCode(factory, "CTL");//装表位置
			Map<String, String> rtp = Tools.mapDicByCode(factory, "RTP");//抄表周期
			Map<String, String> BW = Tools.mapDicByCode(factory, "BW");//表位
			Map<String, String> BJWZ = Tools.mapDicByCode(factory, "BJWZ");//表井位置
			Map<String, String> BJXS = Tools.mapDicByCode(factory, "BJXS");//表井形式
			//客户信息
			Map<String, String> cdl = Tools.mapDicByCode(factory, "CDL");//信用等级
			Map<String, String> ctt = Tools.mapDicByCode(factory, "CTT");//客户类型
			Map<String, String> idt = Tools.mapDicByCode(factory, "IDT");//证件类型
			if(user!=null) {
				user.setStatusName(cus.get(user.getStatus()));
				user.setSplitMeterWayName(mrm.get(user.getSplitMeterWay()));
				user.setMeterTypeName(MMT.get(user.getMeterType()));
				user.setUseWaterObjectName(uws.get(user.getUseWaterObject()));
				user.setMeterUseName(cmc.get(user.getMeterUse()));
				user.setShareWayName(csw.get(String.valueOf(user.getShareWay())));
				user.setSetupMeterLocationName(ctl.get(user.getSetupMeterLocation()));
				user.setMeterReadingCycleName(rtp.get(user.getMeterReadingCycle()));
				user.setMeterBjwzName(BJWZ==null?"":BJWZ.get(user.getMeterBjwz()));
				user.setMeterBjxsName(BJXS==null?"":BJXS.get(user.getMeterBjxs()));
			}
			// 转换状态字段
			if(ctm!=null) {
				ctm.setCreditLevelName(cdl.get(ctm.getCreditLevel()));
				ctm.setCtmTypeName(ctt.get(ctm.getCtmType()));
				ctm.setCertTypeName(idt.get(ctm.getCertType()));
			}
			UserDetailInfoBean detail = new UserDetailInfoBean();
			detail.setMsInfo(msInfo);
			detail.setUserInfo(user);
			detail.setPerson(persons);
			detail.setAccountInfo(account);
			detail.setCtmInfo(ctm);
			info.setData(detail);
			return info;
		} catch (Exception e) {
			logger.error("用户信息查询失败" + e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
		}
	}

}
